import pandas as pd
import numpy as np
import datetime

# 基础设置
np.random.seed(42)
start_date = datetime.datetime(2025, 7, 14)
samples = 1000  # 总样本数
timestamps = [start_date + datetime.timedelta(minutes=i) for i in range(samples)]

# 1. 基础模式 (每日周期)
base_pattern = np.array([
    np.sin(2 * np.pi * i/1440) * 15 + 30  # 1440分钟=24小时
    for i in range(samples)
])

# 2. 工作日/周末差异 (周末负载降低)
day_of_week = np.array([ts.weekday() for ts in timestamps])
weekend_effect = np.where(day_of_week >= 5, -10, 0)  # 周六/日降低10%

# 3. 随机波动
random_noise = np.random.normal(0, 5, samples)

# 4. 突发高负载 (模拟真实场景)
spikes = np.zeros(samples)
for _ in range(8):  # 8次突发
    start = np.random.randint(0, samples-60)
    duration = np.random.randint(10, 60)
    height = np.random.uniform(70, 95)
    spikes[start:start+duration] = height

# 5. 夜间低负载
night_mask = np.array([0 if 6 <= ts.hour <= 22 else -15 for ts in timestamps])

# 组合所有特征
values = base_pattern + weekend_effect + random_noise + spikes + night_mask
values = np.clip(values, 0.1, 99.9)  # 确保在0.1-99.9%范围内

# 创建DataFrame
df = pd.DataFrame({
    'metric_name': ['usage_active'] * samples,
    'value': np.round(values, 4),
    'timestamp_value': [ts.strftime('%Y/%m/%d %H:%M') for ts in timestamps]
})

# 保存为CSV
df.to_csv('simulated_cpu_usage.csv', index=False)